//
//  MZMessageDetailedViewController.m
//  minzuwenhuagong
//
//  Created by WANG CUNRUI on 14-5-23.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZMessageDetailedViewController.h"

@interface MZMessageDetailedViewController ()

@property UIButton *zhanpin;
@property UIButton *weizhi;
@property NSString *imagepath;
@property UIImage *_image1;

@end
@implementation MZMessageDetailedViewController

@synthesize scrollView = _scrollView;
@synthesize detailText = _detailText;
@synthesize messageTitle = _messageTitle;
@synthesize messageTime = _messageTime;
@synthesize messageDept = _messageDept;
@synthesize messageContent = _messageContent;
@synthesize messagePic = _messagePic;

//@synthesize labelsize = _labelsize;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor cyanColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"展讯详情";
    CGRect frame = self.view.bounds;
    CGSize f = frame.size;
    CGFloat h = f.height;
    CGFloat w = f.width;
    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,w,h-114)];
//    _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,350,456)];

    _scrollView.backgroundColor=[UIColor colorWithRed:0.9373 green:0.9216 blue:0.9059 alpha:1.0];
    _scrollView.pagingEnabled=YES;
    [self.view addSubview:_scrollView];
    [self loadimage];
    _label1 = [[UILabel alloc] init ];
    _label1.frame = CGRectMake( 10, 5, 300, 30);
    _label1.layer.cornerRadius = 4;
    _label1.text = _messageTitle;
    _label1.font = [UIFont boldSystemFontOfSize:17];
    //_label1
    [_scrollView addSubview:_label1];
    
    _label1_1 = [[UILabel alloc] init];
    _label1_1.frame = CGRectMake( 10, 0.065*h, 300, 18);
    _label1_1.layer.cornerRadius = 4;
    NSString *time = @"开放时间 : ";
    _label1_1.text = [NSString stringWithFormat:@"%@%@",time,_messageTime];
    _label1_1.font = [UIFont    boldSystemFontOfSize:13];
    [_scrollView addSubview:_label1_1];
    _label1_2 = [[UILabel alloc] init];
    _label1_2.frame = CGRectMake( 10, 0.10*h, 300, 18);
    _label1_2.layer.cornerRadius = 4;
    NSString *dept = @"主办单位 : ";
    _label1_2.text = [NSString stringWithFormat:@"%@%@",dept,_messageDept];
    _label1_2.font = [UIFont boldSystemFontOfSize:13];
    [_scrollView addSubview:_label1_2];
    
    _label3 = [[UILabel alloc] init];
    _label3.frame = CGRectMake(0, 250, 0, 0);
    [_label3 setNumberOfLines:0];
    _label3.lineBreakMode = NSLineBreakByWordWrapping;
    _label3.font = [UIFont boldSystemFontOfSize:13];
    // 测试字串
    NSString *s = _messageContent;
    _label3.text = s;
    NSLog(@"%@",s);
    //设置一个行高上限
    CGSize size = CGSizeMake(320,400);
    //计算实际frame大小，并将label的frame变成实际大小
    NSDictionary *attribute = @{NSFontAttributeName: _label3.font};
    CGSize labelsize = [_label3.text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    _label3.frame = CGRectMake(10,0.55*h, labelsize.width-10, labelsize.height);

    int y = CGRectGetMaxY(_label3.frame);
    [_scrollView setContentSize:(CGSizeMake(CGRectGetWidth(_scrollView.frame), y))];
    
    
   
     _zhanpin = [UIButton buttonWithType:UIButtonTypeCustom];
    _zhanpin.frame = CGRectMake(25, 0.43*h, 120, 50);
    _zhanpin.layer.cornerRadius = 4;
    [_zhanpin setTitle:@"展品" forState:UIControlStateNormal];
    [_zhanpin setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [_zhanpin setImage:[UIImage imageNamed:@"btn_cp.png"] forState:UIControlStateNormal];
    [_zhanpin setImageEdgeInsets:UIEdgeInsetsMake(0, 43, 22, 43)];
    [_zhanpin setTitleEdgeInsets:UIEdgeInsetsMake(0, -53, -25, 0)];
    [_zhanpin setBackgroundColor:[UIColor whiteColor]];
    [_zhanpin addTarget:self action:@selector(bbiClick1:) forControlEvents:UIControlEventTouchUpInside];
    
    _weizhi = [UIButton buttonWithType:UIButtonTypeCustom];
    _weizhi.frame = CGRectMake(175, 0.43*h, 120, 50);
    _weizhi.layer.cornerRadius = 4;
    [_weizhi setTitle:@"位置" forState:UIControlStateNormal];
    [_weizhi setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    [_weizhi setImage:[UIImage imageNamed:@"museum_normal.png"] forState:UIControlStateNormal];
    [_weizhi setImageEdgeInsets:UIEdgeInsetsMake(0, 45, 22, 45)];
    [_weizhi setTitleEdgeInsets:UIEdgeInsetsMake(0, -53, -25, 0)];
    [_weizhi setBackgroundColor:[UIColor whiteColor]];
    [_weizhi addTarget:self action:@selector(bbiClick2:) forControlEvents:UIControlEventTouchUpInside];
    if (h < 568) {
        _label1_1.frame = CGRectMake( 10, 0.075*h, 300, 18);
        _label1_2.frame = CGRectMake( 10, 0.105*h, 300, 18);
        _label1.frame = CGRectMake( 10, 5, 300, 30);
        _zhanpin.frame = CGRectMake(25, 0.48*h, 120, 50);
        _weizhi.frame = CGRectMake(175, 0.48*h, 120, 50);
        _label3.frame = CGRectMake(10,0.6*h, labelsize.width-10, labelsize.height);
    }
    [_scrollView addSubview:_label3];
    [_scrollView addSubview:_zhanpin];
    [_scrollView addSubview:_weizhi];
    
}

-(void)loadimage
{
    CGRect frame = self.view.bounds;
    CGSize f = frame.size;
    CGFloat h = f.height;
    NSString *pic = [NSString stringWithFormat:@"%@", self.messagePic];
    NSLog(@"%@",_messagePic);
    NSRange foundObj = [pic rangeOfString :@";" options:NSCaseInsensitiveSearch];
    if(foundObj.length>0)
    {
        NSRange range = [pic rangeOfString:@";"];
        NSString *b = [pic substringToIndex:range.location];
        
        _imagepath = [NSString stringWithFormat:@"http://www.mzdigital.cn/%@", b];
        

    }else
    {
        _imagepath = [NSString stringWithFormat:@"http://www.mzdigital.cn/%@", pic];
        
      
    }

    NSURL *imageUrl = [NSURL URLWithString:_imagepath];
    NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        self._image1 = [UIImage imageWithData:data];
        if (self._image1 == nil)
        {
            // 使用默认图像占位，既能够保证有图像，又能够保证有地方！
            UIImageView *moImage = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"placeholder.png"]];
            if (h>480) {
                moImage.frame = CGRectMake(0, 0.144*h, 320,170);
            }
            moImage.frame = CGRectMake(0, 0.144*h, 320,150);
            [_scrollView addSubview:moImage];
//            [self loadImageAsyncWithIndexPath];
        }
        else
        {
            UIImageView *moImage = [[UIImageView alloc] initWithImage: self._image1];
            if (h>480) {
                moImage.frame = CGRectMake(0, 0.144*h, 320,170);
            }
            moImage.frame = CGRectMake(0, 0.144*h, 320,150);
            [_scrollView addSubview:moImage];
        }
    }];

}

- (void)bbiClick1:(UIButton *)bbi
{
    _mzmoreViewController = [[MZmoreViewController alloc]init];
    [self.navigationController pushViewController:_mzmoreViewController animated:YES];
    _mzmoreViewController.MZID = _messageID;
    
}

- (void)bbiClick2:(UIButton *)bbi
{
    _weizhiViewController = [[MZweizhi2ViewController alloc]init];
    [self.navigationController pushViewController:_weizhiViewController animated:YES];
    _weizhiViewController.num = _messageTitle;
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
